package cm.mefobe.android.telecomcost.entities;

import java.util.ArrayList;

import cm.mefobe.android.telecomcost.Utility;

public class VoiceRate extends Entity {
	public static final String PROP_TYPE = "VoiceRate";
	private Time _startTime;
	private Time _endTime;
	private TimeIntervall _intervallstart;
	private TimeIntervall _tariffingIntervall;
	private ArrayList<Day> _daysOfWeek;
	private double _connectionCharges;
	private double _minimumCharges;
	private double _maximumCharges;
	private double _fees;
	private long _destinationZoneId;
	private long _destinationCountryId;
	private long _destinationProviderId;
	private TimeIntervall _dureeMax;
	private TimeIntervall _extraIntervallStart;
	private TimeIntervall _extraTariffingIntervall;
	private double _extraFees;
	private String _destination;

	@Override
	public String getName() {
		return "VoiceRate";
	}

	public VoiceRate() {

	}		
	
	public double getFeesPerSecond()
	{
		double fees = getFees() * getTariffingIntervall().getPeriod().inSeconds()/getTariffingIntervall().getOccurence();
		fees = fees + getConnectionCharges();
		fees = fees + getMinimumCharges();
		return fees;
	}
	
	public double getExtraFeesPerSecond()
	{
		double extraFeesInSec = getExtraFees()/getExtraTariffingIntervall().getPeriod().inSeconds() * getExtraTariffingIntervall().getOccurence();				
		return extraFeesInSec;
	}
	
	public double getFeesPerTimeIntervall(TimeIntervall intervall)
	{
		int timeInSec = intervall.getOccurence() * intervall.getPeriod().inSeconds();
		int maxLenghtInSec = getDureeMax().getPeriod().inSeconds() * getDureeMax().getOccurence();
		int timeDiff = 0;
		double fees = 0;
		double feesPerSec = getFees()/getTariffingIntervall().getPeriod().inSeconds()*getTariffingIntervall().getOccurence();
		
		if(timeInSec > maxLenghtInSec)
		{
			timeDiff = timeInSec - maxLenghtInSec;
			fees = feesPerSec * maxLenghtInSec;
			fees = fees + (getExtraFeesPerSecond() * timeDiff);
		}
		else
		{
			fees = feesPerSec * timeInSec;
		}
				
		fees = fees + getConnectionCharges();
		fees = fees + getMinimumCharges();
	
		return fees;
	}

	public void setStartTime(Time _startTime) {
		this._startTime = _startTime;
	}

	public Time getStartTime() {
		return _startTime;
	}

	public void setEndTime(Time _endTime) {
		this._endTime = _endTime;
	}

	public Time getEndTime() {
		return _endTime;
	}

	public void setIntervallstart(TimeIntervall _intervallstart) {
		this._intervallstart = _intervallstart;
	}

	public TimeIntervall getIntervallstart() {
		return _intervallstart;
	}

	public void setTariffingIntervall(TimeIntervall _tariffingIntervall) {
		this._tariffingIntervall = _tariffingIntervall;
	}

	public TimeIntervall getTariffingIntervall() {
		return _tariffingIntervall;
	}

	public void setDaysOfWeek(ArrayList<Day> days) {
		this._daysOfWeek = days;
	}

	public ArrayList<Day> getDaysOfWeek() {
		return _daysOfWeek;
	}

	public void setConnectionCharges(double _connectionCharges) {
		this._connectionCharges = _connectionCharges;
	}

	public double getConnectionCharges() {
		return _connectionCharges;
	}

	public void setMinimumCharges(double _minimumCharges) {
		this._minimumCharges = _minimumCharges;
	}

	public double getMinimumCharges() {
		return _minimumCharges;
	}

	public void setMaximumCharges(double _maximumCharges) {
		this._maximumCharges = _maximumCharges;
	}

	public double getMaximumCharges() {
		return _maximumCharges;
	}

	public void setFees(double _fees) {
		this._fees = _fees;
	}

	public double getFees() {
		return _fees;
	}

	public void setDestinationZoneId(long _destinationZone) {
		this._destinationZoneId = _destinationZone;
	}

	public long getDestinationZoneId() {
		return _destinationZoneId;
	}

	public void setDestinationCountryId(long _destinationCountry) {
		this._destinationCountryId = _destinationCountry;
	}

	public long getDestinationCountryId() {
		return _destinationCountryId;
	}

	public void setDestinationProviderId(long _destinationProvider) {
		this._destinationProviderId = _destinationProvider;
	}

	public long getDestinationProviderId() {
		return _destinationProviderId;
	}

	public void setDureeMax(TimeIntervall timeIntervall) {
		this._dureeMax = timeIntervall;
	}

	public TimeIntervall getDureeMax() {
		return _dureeMax;
	}

	public void setExtraIntervallStart(TimeIntervall _extraIntervallstart) {
		this._extraIntervallStart = _extraIntervallstart;
	}

	public TimeIntervall getExtraIntervallStart() {
		return _extraIntervallStart;
	}

	public void setExtraTariffingIntervall(
			TimeIntervall _extraTariffingIntervall) {
		this._extraTariffingIntervall = _extraTariffingIntervall;
	}

	public TimeIntervall getExtraTariffingIntervall() {
		return _extraTariffingIntervall;
	}

	public void setExtraFees(double _extraFees) {
		this._extraFees = _extraFees;
	}

	public double getExtraFees() {
		return _extraFees;
	}

	@Override
	public String toString() {
		return "VoiceRate [_startTime=" + _startTime + ", _endTime=" + _endTime
				+ ", _intervallstart=" + _intervallstart
				+ ", _tariffingIntervall=" + _tariffingIntervall
				+ ", _daysOfWeek=" + _daysOfWeek + ", _connectionCharges="
				+ _connectionCharges + ", _minimumCharges=" + _minimumCharges
				+ ", _maximumCharges=" + _maximumCharges + ", _fees=" + _fees
				+ ", _destinationZoneId=" + _destinationZoneId
				+ ", _destinationCountryId=" + _destinationCountryId
				+ ", _destinationProviderId=" + _destinationProviderId
				+ ", _dureeMax=" + _dureeMax + ", _extraIntervallStart="
				+ _extraIntervallStart + ", _extraTariffingIntervall="
				+ _extraTariffingIntervall + ", _extraFees=" + _extraFees + "]";
	}

	public void setDestinationLabel(String _destination) {
		this._destination = _destination;
	}

	public String getDestinationLabel() {
		return _destination;
	}

	public String getDatesLabel() {
		return "from " + _startTime + " to " + _endTime + " every "
				+ Utility.formatDays(getDaysOfWeek());
	}
	
	public String getChargesLabel() {
		return _fees + " every " + _tariffingIntervall.toString() + " (" + _extraFees + " every " + _extraTariffingIntervall.toString() + " after " +  _dureeMax.toString() + ")";
	}
}
